﻿Imports System.Activities
Imports System.Data.SqlClient
Imports System.Activities.Presentation.Metadata
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing

Public MustInherit Class MSSQLBaseActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

#Region "プロパティ定義 Property"
    Public Property ConnectionStrings As InArgument(Of String)
    Public Property OutSQLLog As InArgument(Of Boolean) = False
    Public Property Result As OutArgument(Of DataSet)
#End Region

#Region "デリゲート Delegate"
    Protected Delegate Function asyncExecuteDelegate(ByVal script As String, ByVal connect As String, ByVal needRes As Boolean, ByVal needLog As Boolean) As DataSet
#End Region

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Return Nothing
    End Function

    Protected Overrides Sub EndExecute(context As System.Activities.AsyncCodeActivityContext, result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncExecuteDelegate)
        Dim execResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, execResult)
    End Sub

    Public Overridable Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        build.AddCustomAttributes(GetType(MSSQLBaseActivity), "ConnectionStrings",
                                              New RequiredArgumentAttribute,
                                              New CategoryAttribute(My.Resources.CategorySetting),
                                              New DisplayNameAttribute(My.Resources.ConnectionStringsDisplay),
                                              New DescriptionAttribute(My.Resources.ConnectionStringsDescription))
        build.AddCustomAttributes(GetType(MSSQLBaseActivity), "OutSQLLog",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.CategorySetting),
                                              New DisplayNameAttribute(My.Resources.OutSqlLogDisplay),
                                              New DescriptionAttribute(My.Resources.OutSqlLogDesctiption))
        build.AddCustomAttributes(GetType(MSSQLBaseActivity), "Result",
                                              New CategoryAttribute(My.Resources.CategorySetting),
                                              New DisplayNameAttribute(My.Resources.ResultDisplay),
                                              New DescriptionAttribute(My.Resources.ResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)

    End Sub

    Protected Overridable Function ExecuteSQL(ByVal script As String, ByVal connect As String, ByVal needRes As Boolean, ByVal needLog As Boolean) As DataSet
        Return (New SQLExecute).ExecuteSQL(script, connect, needRes)
    End Function

    Protected Overridable Sub OutputLogStrings(ByVal message As String, ByVal connect As String, ByVal severity As Integer, ByVal state As Integer)
        Dim sqlExec = New SQLExecute
        sqlExec.OutputSQLLog(message, severity, state, connect)
    End Sub

End Class
